#include "StdAfx.h"
#include "global.h"
#include "..\resource\ConfigGUI\resource.h"
#include "ConfigGUIDlg.h"
#include "..\GUISys\storeconfig.h"


guiimport_t g_gui_imp;
pfnSetShooter g_set_shooter;
pfnSetShooter2 g_set_shooter2;

EXTERNC void Init_Config_GUI(guiimport_t gui_imp, pfnSetShooter SetShooter,
								pfnSetShooter2 SetShooter2)
{
	g_gui_imp = gui_imp;
	g_set_shooter = SetShooter;
	g_set_shooter2 = SetShooter2;
}

void CenterRectangle(CRect rectParent, CRect* pRcMy)
{
	LONG	lParentCenterX, lParentCenterY;
	LONG	lDlgCenterX, lDlgCenterY, lDeltaCenterX, lDeltaCenterY;

	lParentCenterX = rectParent.left + rectParent.Width() / 2;
	lParentCenterY = rectParent.top + rectParent.Height() / 2;
	lDlgCenterX = pRcMy->left + pRcMy->Width() / 2;
	lDlgCenterY = pRcMy->top + pRcMy->Height() / 2;
	lDeltaCenterX = lParentCenterX - lDlgCenterX;
	lDeltaCenterY = lParentCenterY - lDlgCenterY;
	pRcMy->left += lDeltaCenterX;
	pRcMy->top += lDeltaCenterY;
	pRcMy->right += lDeltaCenterX;
	pRcMy->bottom += lDeltaCenterY;
}

EXTERNC HWND Init_Dialog(RECT rect)
{
	CConfigGUIDlg* pConfig = (CConfigGUIDlg*)AfxGetApp()->GetMainWnd();
	CWnd* wndParent = CWnd::GetActiveWindow();
	
	/* create our dialog */
	if (FALSE == pConfig->Create(CConfigGUIDlg::IDD, wndParent))
	{
		TRACE0("CConfigGUIDlg creation failled.\n");
		return NULL;
	}

	/* adjust our dialog to be centered of the parent window */
	CRect	rectParent(rect);
	CRect	rcDlg;

	pConfig->GetWindowRect(rcDlg);
	CenterRectangle(rectParent, &rcDlg);
	pConfig->MoveWindow(rcDlg, FALSE);

	/* show our dialog */
	pConfig->ShowWindow(SW_SHOW);

	return pConfig->GetSafeHwnd();
}

EXTERNC void Destroy_Dialog(void)
{
	AfxGetApp()->GetMainWnd()->DestroyWindow();
	delete AfxGetApp()->GetMainWnd();
}

EXTERNC pboolean Pre_Trans_Dlg_Msgs(HWND hWnd, MSG* pMsg)
{
	ASSERT(NULL != hWnd);
	CWnd* wndObj = CWnd::FromHandle(hWnd);
	CWnd* wndParent = NULL;

	if (wndObj->IsKindOf(RUNTIME_CLASS(CDialog))) wndParent = wndObj;
	else wndParent = wndObj->GetParent();
	if (NULL == wndParent) wndParent = AfxGetApp()->GetMainWnd();
	
	return (TRUE == wndParent->PreTranslateMessage(pMsg))
			? true : false;
}
